﻿using RuneMasterWin8.Core;
using RuneMasterWin8.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuneMasterWin8.Util
{
    /// <summary>
    /// 分数计算Helper
    /// </summary>
    public class ScoreHelper
    {
        /// <summary>
        /// 判断是否是复式消除
        /// </summary>
        /// <param name="status"></param>
        /// <returns>key-color,value-duplexCount</returns>
        public static Dictionary<int, int> CalRuneDuplex(EliminateStatus status)
        {
            Dictionary<int, int> infos = new Dictionary<int, int>();
            foreach (var color in status.CompleteColors.Distinct())
            {
                int duplex = status.CompleteColors.Count(a => { return a == color; });
                infos.Add(color, duplex);
            }
            return infos;
        }

        /// <summary>
        /// 计算得分
        /// </summary>
        /// <param name="status">消除状态</param>
        /// <returns></returns>
        public static int CalScore(EliminateStatus status, int combo)
        {
            //Dictionary<int, int> duplexInfos = CalRuneDuplex(status);
            //int sumScore = 0;

            //for (int i = 0; i < status.CompleteColors.Count; i++)
            //{
            //    int color = status.CompleteColors[i];
            //    int completeCount = status.CompleteDetails[i];

            //    //如果duplex大于1，则每个消除计算最大分数
            //    int duplex = duplexInfos[color];

            //    int baseScore = 0;
            //    if (duplex > 1)
            //    {
            //        baseScore = GetMaxScore(status, color);
            //    }
            //    else
            //    {
            //        baseScore = 100 * (int)Math.Pow(2, (completeCount - 4));
            //    }
            //    int aimScore = baseScore * combo * duplex;
            //    sumScore += aimScore;
            //}

            //需要根据不同的颜色来统计得分，根据颜色来分类
            Dictionary<int, List<int>> colorInfos = new Dictionary<int, List<int>>();
            int sumScore = 0;

            for (int i = 0; i < status.CompleteColors.Count; i++)
            {
                int color = status.CompleteColors[i];
                int completeCount = status.CompleteDetails[i];

                if (!colorInfos.ContainsKey(color))
                {
                    colorInfos.Add(color, new List<int>());
                }
                colorInfos[color].Add(completeCount);
            }

            foreach (var color in colorInfos.Keys)
            {
                if (colorInfos[color].Count > 1)
                {
                    int mutil = 0;
                    foreach (var complateCount in colorInfos[color])
                    {
                        mutil += (complateCount - 3);
                    }
                    int aimScore = 100 * (int)Math.Pow(2, mutil);

                    if (StageManager.Instance.CurrentUsedTool.ContainsValue(ToolType.Add500))
                    {
                        //分数+500
                        aimScore += 500;
                    }

                    sumScore += aimScore;

                }
                else
                {
                    int aimScore = 100 * (int)Math.Pow(2, (colorInfos[color][0] - 4));
                    if (colorInfos.Count > 1)
                    {
                        aimScore = (int)(aimScore * Math.Pow(2, colorInfos.Keys.ToList().IndexOf(color)));
                    }
                    if (StageManager.Instance.CurrentUsedTool.ContainsValue(ToolType.Add500))
                    {
                        //分数+500
                        aimScore += 500;
                    }
                    sumScore += aimScore;
                }
            }

            sumScore = (int)(sumScore * Math.Pow(2, combo - 1));

            if (StageManager.Instance.CurrentUsedTool.ContainsValue(ToolType.ScoreMutil2))
            {
                //两倍分数
                sumScore = sumScore * 2;
            }

            return sumScore;
        }

        /// <summary>
        /// 计算最大得分
        /// </summary>
        /// <param name="status"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static int GetMaxScore(EliminateStatus status, int color)
        {
            int maxValue = 0;
            for (int i = 0; i < status.CompleteColors.Count; i++)
            {
                int tempColor = status.CompleteColors[i];
                int completeCount = status.CompleteDetails[i];
                int baseScore = 100 * (int)Math.Pow(2, (completeCount - 4));
                if (baseScore > maxValue)
                {
                    maxValue = baseScore;
                }
            }
            return maxValue;
        }
    }
}
